package com.prompt;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.util.Log;

/**
 * 加速度感应器处理
 * */
public class Effecter{

	private static Effecter instance = new Effecter();
	private static final String TAG = "Effecter";
	private Context mContext;

	private MediaPlayer mMediaPlayer;
	
	public Effecter(){
		
	}
	
	public void vibrate(long[] pattern,int repeat){
		Vibrator vibrator;
		try {
			Log.i(TAG, "vibrate ~~ vibrate ~~");
            vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);   
            vibrator.vibrate(pattern, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	public void playVoice(){
		 try {
//	            Uri alert = new //RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
	            mMediaPlayer = new MediaPlayer();
	            AssetFileDescriptor fileDescriptor = mContext.getAssets().openFd("skyCity.mp3");
//	            mMediaPlayer.setDataSource(mContext, alert);
	            mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                        fileDescriptor.getStartOffset(),
                        fileDescriptor.getLength());
	            //final AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
	            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
	            mMediaPlayer.setLooping(true);
	            mMediaPlayer.prepare();
	            mMediaPlayer.start();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
	
	public void stopVoice(){
		try {
            if(this.mMediaPlayer != null) {
               this.mMediaPlayer.stop();
            }
	    } catch (Exception e) {
	            e.printStackTrace();
	    }
	}

	public void setMContext(Context mContext) {
		this.mContext = mContext;
	}

	public static Effecter getInstance() {
		return instance;
	}

	public void test(){
		Log.i(TAG, "test------------ ");
		long[] pattern = {800, 150, 400, 130}; // OFF/ON/OFF/ON.
		this.vibrate(pattern, 2);
		this.playVoice();
	}
}
